package com.gtmonitor.network;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.gtmonitor.GTmonitorService;
import com.gtmonitor.graphics.GraphicsActivity;

public class NetworkView extends View {

    public static final String TAG = "GTmonitor";
    public static int MAXSIZE = 0;
    private static int viewWidth = 0;
    private static int viewHeight = 0;
    private static int viewBottom = 0;
    private static int graphHeight = 0;
    private static int graphBottom = 0;
    private static final int textHeight = 15;
    private static final int gray = 100;
    private static final int white = 255;
    private static float[] netReceiveArray = null;
    private static float[] netTransmitArray = null;
    private static int index = 0;
    private static float range = 1;
    private static NumberFormat formatter = new DecimalFormat("#0.00");
    private static final int rxred = 255;
    private static final int rxgreen = 0;
    private static final int rxblue = 0;
    private static final int txred = 0;
    private static final int txgreen = 255;
    private static final int txblue = 0;

    private static Paint rxPaint = new Paint();
    private static Paint txPaint = new Paint();
    private static Paint paint = new Paint();
    private static Paint textPaint = new Paint();
    private static Paint recordPaint = new Paint();
    
    public NetworkView(Context context) {
        super(context);
        MAXSIZE = GTmonitorService.MAXSIZE;
        rxPaint.setColor(Color.argb(100, rxred, rxgreen, rxblue));
        txPaint.setColor(Color.argb(100, txred, txgreen, txblue));
        textPaint.setColor(Color.argb(100, white, white, white));
        paint.setColor(Color.argb(50, gray, gray, gray));
        recordPaint.setColor(Color.RED);
    }
    
    public NetworkView(Context context, AttributeSet attr, int defStyle) {
        super(context, attr, defStyle);
        MAXSIZE = GTmonitorService.MAXSIZE;
        rxPaint.setColor(Color.argb(100, rxred, rxgreen, rxblue));
        txPaint.setColor(Color.argb(100, txred, txgreen, txblue));
        textPaint.setColor(Color.argb(100, white, white, white));
        paint.setColor(Color.argb(50, gray, gray, gray));
        recordPaint.setColor(Color.RED);
    }
    
    public NetworkView(Context context, AttributeSet attr) {
        super(context, attr); 
        MAXSIZE = GTmonitorService.MAXSIZE;
        rxPaint.setColor(Color.argb(100, rxred, rxgreen, rxblue));
        txPaint.setColor(Color.argb(100, txred, txgreen, txblue));
        textPaint.setColor(Color.argb(100, white, white, white));
        paint.setColor(Color.argb(50, gray, gray, gray));
        recordPaint.setColor(Color.RED);
    }
    
    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLACK);
        viewWidth = getWidth();
        viewWidth = viewWidth > MAXSIZE ? MAXSIZE : viewWidth;
        viewHeight = getHeight();
        viewBottom = getBottom();
        viewBottom -= (viewBottom - viewHeight);
        graphHeight = viewHeight - textHeight;
        graphBottom = viewBottom - textHeight;
         
        
        netReceiveArray = GTmonitorService.netReceiveUtilArray;
        if (netReceiveArray == null) return;
        netTransmitArray = GTmonitorService.netTransmitUtilArray;
        if (netTransmitArray == null) return;
        index = GTmonitorService.index;
        range = GTmonitorService.netRange; if (range == 0) return;
        //Log.d(TAG, "range " + range + " graphBottom " + graphBottom + " viewBottom " + viewBottom);
        int sid = (index - 1 + MAXSIZE) % MAXSIZE;
        //int to, from;
        for (int i=sid, c=0; c<viewWidth; ++c) {
            /*
            to = i - 1; if (to < 0) to = MAXSIZE - 1;
            from = i;
            canvas.drawLine(c,     graphBottom - (graphHeight * netReceiveArray[from] / range), 
                            c + 1, graphBottom - (graphHeight * netReceiveArray[to] / range), rxPaint);
            canvas.drawLine(c,     graphBottom - (graphHeight * netTransmitArray[from] / range), 
                            c + 1, graphBottom - (graphHeight * netTransmitArray[to] / range), txPaint);
            */
            canvas.drawLine(c, graphBottom, c, graphBottom - (graphHeight * netReceiveArray[i] / range), rxPaint); 
            canvas.drawLine(c, graphBottom, c, graphBottom - (graphHeight * netTransmitArray[i] / range), txPaint); 
            i = (i - 1 + MAXSIZE) % MAXSIZE;
        }
        
        float zero = graphBottom;
        float twentyfive = graphBottom - graphHeight * 0.25f;
        float fifty = graphBottom - graphHeight * 0.5f;
        float seventyfive = graphBottom - graphHeight * 0.75f;
        float hundred = graphBottom - graphHeight;
        canvas.drawLine(0, zero, viewWidth, zero, paint);
        canvas.drawLine(0, twentyfive, viewWidth, twentyfive, paint);
        canvas.drawLine(0, fifty, viewWidth, fifty, paint);
        canvas.drawLine(0, seventyfive, viewWidth, seventyfive, paint);
        canvas.drawLine(0, hundred, viewWidth, hundred, paint);
        //canvas.drawText(percent100, 0, graphBottom - graphHeight + 10, textPaint);
        canvas.drawText(formatter.format(range) + "K/s", 0, graphBottom - graphHeight + 10, textPaint);
        //canvas.drawText(percent50, 0, fifty + 5, textPaint);
        canvas.drawText("0min", 0, viewBottom, textPaint);
        canvas.drawText("1.5", 90, viewBottom, textPaint);
        canvas.drawText("3min", 180, viewBottom, textPaint);
        canvas.drawText("4.5", 270, viewBottom, textPaint);
        canvas.drawText("6min", 360, viewBottom, textPaint);
        canvas.drawText("7.5", 450, viewBottom, textPaint);
        if (GTmonitorService.networkLogOn && GraphicsActivity.loggable) {
            canvas.drawCircle(viewWidth - 6, hundred + 6, 6, recordPaint);
        }
    }

}
